home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 016a / 4date.zip / 4DATE.BTM next >
Text File  |  1991-11-07  |  8KB  |  301 lines

  1. @echo off
  2. :       4DATE - implements various useful date functions.
  3. :
  4. :       Usage:
  5. :               4DATE [date-function argument...]
  6. :
  7. :       Returns:
  8. :               4DATE functions set the environment variable
  9. :               "_$" to the return value of the function.
  10. :               Primitive functions ONLY set "_$", higher
  11. :               level interfaces display results.
  12. :
  13. :       History:
  14. :       07-Nov-91    MGiguere    DOW now uses 4DOS %@INT[] func.
  15. :       31-Oct-91    MGiguere    %_date default for DDATE & DIFF
  16. :       25-Oct-91    MGiguere    Added Day-Of-Week function.
  17. :       24-Oct-91    MGiguere    Created.
  18. :
  19. iff "%debug" NE "" then
  20.     echo 4DATE:
  21.     echo on
  22. endiff
  23.  
  24. iff "%1" EQ "" then
  25.    gosub HELP
  26. else
  27.    gosub %1
  28. endiff
  29. quit
  30.  
  31. :CJUL
  32. :   CJUL - 4DATE CJUL date
  33. :          Compute & Return the "Julian" day of year for the specified date.
  34. :
  35. :   Primitive function.
  36. :
  37. set _$=%@eval[1+%@date[%2]-%@date[01-01-%@substr[%2,6]]]
  38. return
  39.  
  40. :DDATE
  41. :       DDATE - 4DATE DDATE integer [aDate]
  42. :               DeltaDate returns the actual date for a given
  43. :               number of days before or after a give date.
  44. :               If aDate not specified todays date is default.
  45. :
  46. :       Primitive function.
  47. :
  48. iff .%3==. then
  49.    set _$=%@makedate[%@eval[%@date[%_date]+%2]]
  50. else
  51.    set _$=%@makedate[%@eval[%@date[%3]+%2]]
  52. endiff
  53. return
  54.  
  55. :DIFF
  56. :       DIFF - DIFF date1 [date2]
  57. :              compute the difference between two dates as 
  58. :              an absolute value. If date2 not specified
  59. :              DIFF assumes the computation is relative
  60. :              to the current system date (%_date).
  61. :
  62. :       Primitive function.
  63. :
  64. iff .%3==. then
  65.    set _$=%@eval[%@date[%_date]-%@date[%2]]
  66. else
  67.    set _$=%@eval[%@date[%2]-%@date[%3]]
  68. endiff
  69. iff "%@substr[%_$,0,1]" EQ "-" then set _$=%@substr[%_$,1]
  70. return
  71.  
  72. :DJD
  73. :       DJD - 4DATE DJD julianDate [year]
  74. :             Display Julian day as Date - convert Julian date to calander
  75. :             date. If no year is specified the current year is assumed.
  76. :
  77. :       User level function
  78. :
  79. call 4date.btm j2d %2&
  80. echo Calander Date: %_$
  81. return
  82.  
  83. :DSTRUCT
  84. :       DSTRUCT - 4DATE DSTRUCT date dv1 dv2 dv3
  85. :                 Returns a Date Structure.
  86. :                 Takes date and returns with the environment
  87. :                 varibles set to date values. The args dv1,2,3
  88. :                 are the names of environment variables to
  89. :                 contain the parts of the date, i.e. day, month, year.
  90. :
  91. :       Primitive function
  92. :
  93. set %3=%@substr[%2,0,2]
  94. set %4=%@substr[%2,3,2]
  95. set %5=%@substr[%2,6,2]
  96. return
  97.  
  98. :DOW
  99. :       DOW -   4DATE DOW [date [century]]
  100. :               Uses Zellers congruence to compute the day-of-the-week.
  101. :               DOW's implementation assumes US date conventions.
  102. :               Change the call to DSTRUCT in DOW to reorder the
  103. :               month and day variables as apporpriate to your
  104. :               date format.
  105. :       Primitive function
  106. :
  107. : cheat if no arg, i.e. today
  108. iff .%2==. then
  109.     set _$=%_dow
  110.     return
  111. endiff
  112.  
  113. :  set up local stack data structures
  114. set _d0=Sun
  115. set _d1=Mon
  116. set _d2=Tue
  117. set _d3=Wed
  118. set _d4=Thu
  119. set _d5=Fri
  120. set _d6=Sat
  121.  
  122. :  this is ugly but necessary since 4DOS dates to not
  123. :  include the century.
  124. iff "%3" EQ "" then 
  125.     set _$c=19
  126. else
  127.     set _$c=%3
  128. endiff
  129.  
  130. :   Change this call for different data formats
  131. call 4date.btm dstruct %2 $m $d $y
  132.  
  133. iff "%$m" EQ "01" .OR. "%$m" EQ "02" then
  134.      set $m=%@eval[%$m + 12]
  135.      iff "%$y" GT "00" then
  136.         set $y=%@eval[%$y-1]
  137.      else
  138.         set %$y=99
  139.         set _$c=%@eval[%_$c-1]
  140.      endiff
  141. endiff
  142.  
  143. set _$=%$d
  144. :  The following code implements Zellers algorithm.
  145.  
  146. set _$=%@eval[%_$+%@int[%@eval[((%$m+1)*26)/10]]]
  147.  
  148. set _$=%@eval[%_$+%$y]
  149.  
  150. :  correct for leap years
  151. set _$=%@eval[%_$+(%@int[%@eval[%$y/4]])]
  152.  
  153. :  correct for leap centuries
  154. set _$=%@eval[%_$+(%@int[%@eval[%_$c/4]])]
  155.  
  156. set _$=%@eval[%_$-%_$c-%_$c]
  157.  
  158. :  while loop kludge
  159. :LOOP
  160. if %_$ GE 0 goto ENDLOOP
  161. set _$=%@eval[%_$+7]
  162. goto loop
  163. :ENDLOOP
  164.  
  165. set _$=%@eval[%_$ %% 7]
  166. if %_$ == 0 set _$=7
  167. set _$=%@eval[%_$-1]
  168.  
  169. :  return day-of-week
  170. set _$=%[_d%[_$]]
  171. :  clean up local stack data
  172. unset _$c
  173. unset _d0
  174. unset _d1
  175. unset _d2
  176. unset _d3
  177. unset _d4
  178. unset _d5
  179. unset _d6
  180. unset $d
  181. unset $m
  182. unset $y
  183. return
  184.  
  185. :IS
  186. :       IS - 4DATE IS integer
  187. :            Displays the date for the given number of
  188. :            days into the past or future from today.
  189. :
  190. :       User function.
  191. :
  192. call 4date.btm ddate %2 %_date
  193. echo Date is: %_$
  194. return
  195.  
  196. :J2D
  197. :   J2D - 4DATE J2D julianDate [year]
  198. :         Converts a Julian day to a calander date.
  199. :         If no year is specified the current year
  200. :         is assumed.
  201. :
  202. :   Primitive function.
  203. :
  204. iff "%3" EQ "" then
  205.     set _$y=%@substr[%_date,6]
  206. else
  207.     set _$y=%3
  208. endiff
  209. set _$=%@makedate[%@eval[%2+%@date[01-01-%_$y]-1]]
  210. unset _$y
  211. return
  212.  
  213. :FDOW
  214. :    FDOW - 4DATE FDOW [date]
  215. :       Fast Day-Of-Week, returns day-of-week FAST.
  216. :       CAUTION - this function may leave system date
  217. :       in an incorrect state if interrupted before
  218. :       it completes.  I cheat by setting the system
  219. :       date to the date for which the DOW is desired,
  220. :       then resetting the date.  DOW is slower, but
  221. :       safer if you absolutely depend on your system
  222. :       date being correct.
  223. :
  224. :   Primitive function
  225. :
  226. iff .%2==. then
  227.     set _$=%_dow
  228. else
  229.     set _$d=%_date
  230.     *date %2
  231.     set _$=%_dow
  232.     *date %_$d
  233.     unset _$d
  234. endiff
  235. return
  236.  
  237. :JDAY
  238. :   JDAY - 4DATE JDAY
  239. :          Displays todays "Julian" day of the year.
  240. :
  241. :   User level function.
  242. :
  243. call 4date.btm cjul %_date
  244. echo Julian date: %_$
  245. return
  246.  
  247. :HELP
  248. :       HELP - Displays summary of 4DATE functions.
  249. :
  250. :       User level function.
  251. :
  252. text
  253.     4DATE - Implements various useful date functions.
  254.             Primitives always return values in _$
  255.  
  256.     Usage:  4DATE [function argument...]
  257.  
  258.     CJUL - 4DATE CJUL date (Primitive function)
  259.            Return the "Julian" day of year for the specified date.
  260.  
  261.     DDATE - 4DATE DDATE integer [aDate] (Primitive function)
  262.             DeltaDate returns the actual date for a given
  263.             number of days before or after a given date.
  264.             If aDate not specified todays date is default.
  265.  
  266.     DIFF - 4DATE DIFF date1 [date2] (Primitive function)
  267.            Compute the absolute difference between two dates.
  268.            If date2 not specified DIFF assumes the computation
  269.            is relative to the current system date (%_date).
  270.  
  271.     DJD - 4DATE DJD julianDate [year] (User level function)
  272.           Display Julian date as calander Date.  If no year is
  273.           specified the current year is assumed.
  274.  
  275.     DSTRUCT - 4DATE DSTRUCT date dv1 dv2 dv3 (Primitive function)
  276.               Takes a date and returns with the given environment
  277.               varibles set to date values.
  278.  
  279.     DOW -   4DATE DOW [date [century]] (Primitive function)
  280.             Uses Zellers congruence to compute the day-of-the-week.
  281.             U.S. date form assumed (see function for changing this).
  282.  
  283.     FDOW - Fast Day-Of-Week (Primitive function)
  284.            CAUTION - this function may leave system date
  285.            in an incorrect state if interrupted.
  286.  
  287.     HELP - Displays this page.
  288.  
  289.     IS - 4DATE IS integer (User level function)
  290.          Displays the date for the given number of
  291.          days into the past or future from today.
  292.  
  293.     J2D - 4DATE J2D julianDate [year] (Primitive function)
  294.           Converts a Julian day to a calander date.
  295.           The current year is the default.
  296.  
  297.     JDAY - 4DATE JDAY (User level function)
  298.            Displays todays "Julian" day of the year.
  299. endtext
  300.  
  301.